<?php

declare(strict_types=1);

namespace App\Admin\Controller\System\DataCenter;

use App\Admin\Annotation\Auth;
use App\Admin\Annotation\Permission;
use App\Admin\Request\System\DictData\DictDataCreateRequest;
use App\Admin\Request\System\DictData\DictDataStatusRequest;
use App\Admin\Service\System\SystemDictDataService;
use DI\Attribute\Inject;
use Newbie\Annotation\Attributes\Route\Controller;
use Newbie\Annotation\Attributes\Route\DeleteMapping;
use Newbie\Annotation\Attributes\Route\GetMapping;
use Newbie\Annotation\Attributes\Route\PostMapping;
use Newbie\Annotation\Attributes\Route\PutMapping;
use Newbie\NewbieController;
use support\Request;
use support\Response;

/**
 * 字典类型控制器
 * Class LogsController
 * @package App\system\Controller\DataCenter
 */
#[Controller(prefix: "system/dataDict"), Auth]
class DictDataController extends NewbieController {
    #[Inject]
    protected SystemDictDataService $service;

    /**
     * 列表
     * @param Request $request
     * @return Response
     */
    #[GetMapping("index"), Permission("system:dataDict:index")]
    public function index (Request $request): Response {
        return $this->success($this->service->getPageList($request->all()));
    }

    /**
     * 快捷查询一个字典
     * @param Request $request
     * @return Response
     */
    #[GetMapping("list")]
    public function list (Request $request): Response {
        return $this->success($this->service->getList($request->all()));
    }

    /**
     * 快捷查询多个字典
     * @param Request $request
     * @return Response
     */
    #[GetMapping("lists")]
    public function lists (Request $request): Response {
        return $this->success($this->service->getLists($request->all()));
    }

    /**
     * 清除字典缓存
     * @return Response
     */
    #[PostMapping("clearCache"), Permission("system:dataDict:clearCache")]
    public function clearCache (): Response {
        return $this->service->clearCache() ? $this->success() : $this->error();
    }

    /**
     * 回收站列表
     * @param Request $request
     * @return Response
     */
    #[GetMapping("recycle"), Permission("system:dataDict:recycle")]
    public function recycle (Request $request): Response {
        return $this->success($this->service->getPageListByRecycle($request->all()));
    }

    /**
     * 新增字典类型
     * @param DictDataCreateRequest $request
     * @return Response
     */
    #[PostMapping("save"), Permission("system:dataDict:save")]
    public function save (DictDataCreateRequest $request): Response {
        return $this->success(['id' => $this->service->save($request->all())]);
    }

    /**
     * 获取一个字典类型数据
     * @param int $id
     * @return Response
     */
    #[GetMapping("read/{id}"), Permission("system:dataDict:read")]
    public function read (int $id): Response {
        return $this->success($this->service->read($id));
    }

    /**
     * 更新一个字典类型
     * @param int $id
     * @param DictDataCreateRequest $request
     * @return Response
     */
    #[PutMapping("update/{id}"), Permission("system:dataDict:update")]
    public function update (int $id, DictDataCreateRequest $request): Response {
        return $this->service->update($id, $request->all()) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量字典数据
     * @param String $ids
     * @return Response
     */
    #[DeleteMapping("delete/{ids}"), Permission("system:dataDict:delete")]
    public function delete (string $ids): Response {
        return $this->service->delete($ids) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量真实删除字典 （清空回收站）
     * @param String $ids
     * @return Response
     */
    #[DeleteMapping("realDelete/{ids}"), Permission("system:dataDict:realDelete")]
    public function realDelete (string $ids): Response {
        return $this->service->realDelete($ids) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量恢复在回收站的字典
     * @param String $ids
     * @return Response
     */
    #[PutMapping("recovery/{ids}"), Permission("system:dataDict:recovery")]
    public function recovery (string $ids): Response {
        return $this->service->recovery($ids) ? $this->success() : $this->error();
    }

    /**
     * 更改字典状态
     * @param DictDataStatusRequest $request
     * @return Response
     */
    #[PutMapping("changeStatus"), Permission("system:dataDict:changeStatus")]
    public function changeStatus (DictDataStatusRequest $request): Response {
        return $this->service->changeStatus((int)$request->input('id'), (string)$request->input('status'))
            ? $this->success() : $this->error();
    }
}
